﻿using SDAU.GCT.OA.DALFactory;
using SDAU.GCT.OA.IDal;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;

namespace SDAU.GCT.OA.BLL
{
    public abstract class BaseService<T> where T : class, new()

    {
        /// <summary>
        /// 父类要逼迫子类 在调用父类方法之前 给父类的一个属性赋值
        /// 
        /// </summary>
        public IBaseDal<T> CurrentDal { get; set; }

        //DbSession拥有所有dal的实例
        public IDbSession DbSession { get; set; }

        //子类重写 给父类的一个属性赋值
        public abstract void GetCurrentDal();

        #region 异步CUD
  
        public async Task<T> AddAsync(T t)
        {
            CurrentDal.Add(t);
            //数据提交的权利从数据库访问层提到了业务逻辑层
            //  DbSession.Savechanges();

            //异步方法进行操作
            await DbSession.SaveChangesAsync();

            //可以实现批量提交
            return t;
        }
        public async Task<int> deleteListAsync(List<int> ids)
        {
            foreach (var id in ids)
            {
                CurrentDal.Delete(id);
            }
            return await DbSession.SaveChangesAsync();
        }
        public async Task<bool> DeleteAsync(int id)
        {
            CurrentDal.Delete(id);
            return await DbSession.SaveChangesAsync() > 0;
        }
        public async Task<bool> DeleteAsync(T t)
        {
            CurrentDal.Delete(t);
            return await DbSession.SaveChangesAsync() > 0;
        }
        //对数据进行逻辑删除
        public async Task<int> DeleteListByLogiclAsync(List<int> ids)
        {
            CurrentDal.DeleteListByLogicl(ids);
            return await DbSession.SaveChangesAsync();

        }

        public async Task<bool> UpdateAsync(T t)
        {
            CurrentDal.Update(t);
            return await DbSession.SaveChangesAsync() > 0;
        } 
        #endregion

        public IQueryable<T> GetEntities(Expression<Func<T, bool>> WhereLambda)
        {
            return CurrentDal.GetEntities(WhereLambda);
        }
        public IQueryable<T> GetTInfoPage<S>(int pageSize, int pageIndex, out int total,
           Expression<Func<T, bool>> WhereLambda, Expression<Func<T, S>> OrderbyLambda, bool isAsc)
        {
            return CurrentDal.GetTInfoPage(pageSize, pageIndex, out total, WhereLambda, OrderbyLambda, isAsc);
        }
     
        #region 非异步CUD

        public T Add(T t)
        {
            CurrentDal.Add(t);
            //数据提交的权利从数据库访问层提到了业务逻辑层
            DbSession.Savechanges();

            //可以实现批量提交
            return t;
        }
        public int deleteList(List<int> ids)
        {
            foreach (var id in ids)
            {
                CurrentDal.Delete(id);
            }
            return DbSession.Savechanges();
        }
        public bool Delete(int id)
        {
            CurrentDal.Delete(id);
            return DbSession.Savechanges() > 0;
        }
        public bool Delete(T t)
        {
            CurrentDal.Delete(t);
            return DbSession.Savechanges() > 0;
        }
        //对数据进行逻辑删除
        public int DeleteListByLogicl(List<int> ids)
        {
            CurrentDal.DeleteListByLogicl(ids);
            return DbSession.Savechanges();

        }

        public bool Update(T t)
        {
            CurrentDal.Update(t);
            return DbSession.Savechanges() > 0;
        } 
        #endregion
    }
}
